#!/bin/bash
git pull
cur_date=$(date +%Y%m%d%H%M%S)
docker build -t bulingbuling:${cur_date} .

# 分别定义服务的端口和工作进程数
declare -A services_ports=(
  ["personal_service"]=6003 
  ["game_manager_service"]=6004
  ["advance_order_service"]=6005
)

declare -A services_workers=(
  ["personal_service"]=1
  ["game_manager_service"]=1
  ["advance_order_service"]=1
)

for service_name in "${!services_ports[@]}"; do
  port=${services_ports[$service_name]}
  workers=${services_workers[$service_name]}

  # 检查容器是否存在
  if docker inspect $service_name > /dev/null 2>&1; then
    echo "Stopping and removing existing container: $service_name"
    docker stop $service_name
    docker rm $service_name
  else
    echo "Container $service_name does not exist, skipping stop and remove."
  fi

  echo "Running new container for $service_name on port $port with $workers workers"
  docker run -e TZ=Asia/Shanghai -e SERVICE_NAME=$service_name -d -p $port:$port --restart=always --name $service_name bulingbuling:${cur_date} /bin/bash -c "cd /app && gunicorn -k eventlet -w $workers -b 0.0.0.0:$port main:app"
done
